/*
 * XIconButton.java
 * 
 * Copyright (c) 2005 - 2007
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.xfc.components;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;

import org.xfc.util.XListenerManager;
import org.xfc.util.XUtils;

import com.jgoodies.forms.layout.FormLayout;

/**
 * A button implementation that only displays an icon. It is sized to the icon's
 * size and has some options for rollover / pressed / disabled icons.
 * 
 * @author Devon Carew
 */
public class XIconButton
	extends JComponent
{
	private Icon	icon;
	private Icon	pushedIcon;
	private Icon	disabledIcon;
	private Icon	rolloverIcon;
	
	private JLabel	iconLabel;
	
	private boolean	hover;
	private boolean	pressed;
	
	private XListenerManager 	listenerManager = new XListenerManager();
	private Action				action;
	
	public XIconButton(Action action)
	{
		this((Icon)action.getValue(Action.SMALL_ICON));
		
		this.action = action;
		
		if (action.getValue(Action.SHORT_DESCRIPTION) != null)
			iconLabel.setToolTipText((String)action.getValue(Action.SHORT_DESCRIPTION));
	}
	
	public XIconButton(Icon icon)
	{
		setLayout(new FormLayout("pref:grow", "pref:grow"));
		
		iconLabel = new JLabel();
		
		add(iconLabel, "1, 1, c, c");
		
		setOpaque(false);
		
		setIcon(icon);
		
		iconLabel.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent event) {
				hover = true;
				updateIcon();
			}
			public void mouseExited(MouseEvent event) {
				hover = false;
				updateIcon();
			}
			public void mouseClicked(MouseEvent event) {
				fireActionEvent();
			}
			public void mousePressed(MouseEvent event) {
				pressed = true;
				updateIcon();
			}
			public void mouseReleased(MouseEvent event) {
				pressed = false;
				updateIcon();
			}
		});
	}

	public Icon getDisabledIcon()
	{
		return disabledIcon;
	}

	public void setDisabledIcon(Icon disabledIcon)
	{
		this.disabledIcon = disabledIcon;
		
		updateIcon();
	}

	public Icon getIcon()
	{
		return icon;
	}

	public void setIcon(Icon icon)
	{
		this.icon = icon;
		
		//if (disabledIcon == null && icon instanceof ImageIcon)
			setDisabledIcon(XUtils.createDisabledIcon((ImageIcon)icon));
		
		//if (rolloverIcon == null && icon instanceof ImageIcon)
			setRolloverIcon(XUtils.createHoverIcon((ImageIcon)icon));
		
		//if (pushedIcon == null)
			setPushedIcon(XUtils.createPressedIcon(icon));
		
		updateIcon();
	}

	public Icon getPushedIcon()
	{
		return pushedIcon;
	}

	public void setPushedIcon(Icon pushedIcon)
	{
		this.pushedIcon = pushedIcon;
		
		updateIcon();
	}

	public Icon getRolloverIcon()
	{
		return rolloverIcon;
	}

	public void setRolloverIcon(Icon rolloverIcon)
	{
		this.rolloverIcon = rolloverIcon;
		
		updateIcon();
	}
	
	private void updateIcon()
	{
		if (!isEnabled())
		{
			iconLabel.setIcon(getDisabledIcon());
		}
		else
		{
			if (pressed && getPushedIcon() != null)
			{
				iconLabel.setIcon(getPushedIcon());
			}
			else if (hover)
			{
				iconLabel.setIcon(getRolloverIcon() != null ? getRolloverIcon() : getIcon());
			}
			else
			{
				iconLabel.setIcon(getIcon());
			}
		}
	}
	
	public String getToolTipText()
	{
		return iconLabel.getToolTipText();
	}

	public void setToolTipText(String text)
	{
		super.setToolTipText(text);
		iconLabel.setToolTipText(text);
	}

	public void setEnabled(boolean enabled)
	{
		super.setEnabled(enabled);
		
		updateIcon();
	}
	
	public void addActionListener(ActionListener listener)
	{
		listenerManager.addListener(listener);
	}
	
	public void removeActionListener(ActionListener listener)
	{
		listenerManager.removeListener(listener);
	}
	
	void fireActionEvent()
	{
		if (!isEnabled())
			return;
		
		ActionEvent event = new ActionEvent(this, 0, "");
		
		listenerManager.notifyListeners("actionPerformed", event);
		
		if (action != null)
			action.actionPerformed(event);
	}
	
}
